package com.example.mall_system.controller;

import com.example.mall_system.common.Result;
import com.example.mall_system.service.Impl.CartItemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartItemServiceImpl cartItemService;

    // 获取当前用户购物车列表
    @GetMapping
    public Result<?> getCartItems(@RequestParam Long userId) {
        return cartItemService.getCartItemsByUserId(userId);
    }

    // 添加商品到购物车
    @PostMapping("/items")
    public Result<?> addCartItem(
            @RequestParam Long userId,
            @RequestParam Long productId,
            @RequestParam Integer quantity) {
        return cartItemService.addCartItem(userId, productId, quantity);
    }

    // 更新购物车商品数量
    @PutMapping("/items/{id}")
    public Result<?> updateCartItemQuantity(
            @PathVariable Integer id,
            @RequestParam Integer quantity,
            @RequestParam Long userId) {
        return cartItemService.updateCartItemQuantity(id, quantity, userId);
    }

    // 删除购物车商品
    @DeleteMapping("/items/{id}")
    public Result<?> deleteCartItem(
            @PathVariable Integer id,
            @RequestParam Long userId) {
        return cartItemService.deleteCartItem(id, userId);
    }

    // 清空购物车
    @DeleteMapping
    public Result<?> clearCart(@RequestParam Long userId) {
        return cartItemService.clearCart(userId);
    }
}